package cn.edu.stu.syllabus.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

import cn.edu.stu.syllabus.R;

/**
 * Created by daidaijie on 2017/4/15.
 * 用于给东门外卖添加，放入购物车中的动画
 */

public class AddToMenuViewAnimation {

    /**
     * 开始动画动作
     *
     * @param context
     * @param aniLayout         负责进行动画的Layout
     * @param addButton         加号 Button
     * @param shoppingImageView 购物车ImageView
     */
    public static void start(final Context context, final ViewGroup aniLayout,
                             View addButton, final View shoppingImageView) {
        // 获取按钮位置
        int[] location = new int[2];
        addButton.getLocationInWindow(location);

        // 创建和加号Button一致的Button，并设置文字为1
        final TextView plusOneTextView = new TextView(context);
        plusOneTextView.setText(Html.fromHtml("<b>1</b>"));
        plusOneTextView.setGravity(Gravity.CENTER);
        GradientDrawable drawable = (GradientDrawable) context.getResources()
                .getDrawable(R.drawable.bg_add_dishes);
        drawable.setColor(ContextCompat.getColor(context, R.color.colorAccent));
        plusOneTextView.setBackgroundDrawable(drawable);
        plusOneTextView.setTextColor(context.getResources().getColor(R.color.material_white));
        plusOneTextView.setTextSize(12);

        // 将plusOneTextView添加到执行动画的布局上
        aniLayout.addView(plusOneTextView);

        // 设置plusOneTextView的初始位置和加号位置重合
        int width = DensityUtil.dip2px(24);
        plusOneTextView.getLayoutParams().width = width;
        plusOneTextView.getLayoutParams().height = width;

        int lf = location[0] + width / 2;
        int tf = location[1] + width / 2;
        plusOneTextView.setX(lf);
        plusOneTextView.setY(tf);

        // 获取购物车ImageView的位置
        int[] shoppingLocation = new int[2];
        shoppingImageView.getLocationInWindow(shoppingLocation);

        // 添加ObjectAnimator，给plusOneTextView设置平抛运动
        ObjectAnimator anix = ObjectAnimator.ofFloat(
                plusOneTextView, "x", lf, shoppingLocation[0] + width / 2
        );

        ObjectAnimator aniy = ObjectAnimator.ofFloat(
                plusOneTextView, "y", tf, shoppingLocation[1]
        );
        anix.setInterpolator(new LinearInterpolator());
        aniy.setInterpolator(new AccelerateInterpolator());
        final AnimatorSet dumpAni = new AnimatorSet();
        dumpAni.play(anix).with(aniy);
        dumpAni.setDuration(500);
        dumpAni.start();

        // 添加动画回调，在plusOneTextView结束动画的时候，将购物车ImageView放大
        dumpAni.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                aniLayout.removeView(plusOneTextView);
                ObjectAnimator aniScaleX = ObjectAnimator.ofFloat(
                        shoppingImageView, "scaleX", 1.1f, 1.0f
                );
                ObjectAnimator aniScaleY = ObjectAnimator.ofFloat(
                        shoppingImageView, "scaleY", 1.1f, 1.0f
                );
                AnimatorSet scaleAni = new AnimatorSet();
                scaleAni.play(aniScaleX).with(aniScaleY);
                scaleAni.setDuration(300);
                scaleAni.setInterpolator(new AnticipateOvershootInterpolator());
                scaleAni.start();
                dumpAni.removeListener(this);
            }
        });
    }
}
